package com.wmh.baseservice.merchant.service.impl;

import com.wmh.baseservice.admin.pojo.AdProfit;
import com.wmh.baseservice.admin.service.AdProfitService;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.mybatisplus.methods.CommonServiceImpl;
import com.wmh.baseservice.common.utils.DateFormatTemplate;
import com.wmh.baseservice.merchant.mapper.MerchantActivityMapper;
import com.wmh.baseservice.merchant.pojo.MerchantActivity;
import com.wmh.baseservice.merchant.pojo.MerchantIncomeExpend;
import com.wmh.baseservice.merchant.pojo.MerchantInfo;
import com.wmh.baseservice.merchant.service.MerchantActivityService;
import com.wmh.baseservice.merchant.service.MerchantIncomeExpendService;
import com.wmh.baseservice.merchant.service.MerchantInfoService;
import com.wmh.baseservice.system.pojo.SysParams;
import com.wmh.baseservice.system.service.SysParamsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author wmh generator
 * @since 2022-04-08
 */
@Service
public class MerchantActivityServiceImpl extends CommonServiceImpl<MerchantActivityMapper, MerchantActivity> implements MerchantActivityService {

    @Resource
    private SysParamsService sysParamsService;

    @Resource
    private MerchantInfoService merchantInfoService;

    @Resource
    private AdProfitService adProfitService;

    @Resource
    private MerchantIncomeExpendService merchantIncomeExpendService;

    @Override
    public boolean activitiesIsInProgress(MerchantActivity merchantActivity) {
        LocalDateTime now = LocalDateTime.now(DateFormatTemplate.ZONE_SHANGHAI);
        LocalDateTime start = LocalDateTime.ofInstant(merchantActivity.getOpenTime().toInstant(), DateFormatTemplate.ZONE_SHANGHAI);
        LocalDateTime end = LocalDateTime.ofInstant(merchantActivity.getOverTime().toInstant(), DateFormatTemplate.ZONE_SHANGHAI);
        if(now.isAfter(start) && now.isBefore(end)){
            return true;
        }
        return false;
    }

    @Override
    public void signUpTimeCheck(String errMsgPrefix, MerchantActivity merchantActivity) {
        LocalDateTime now = LocalDateTime.now(DateFormatTemplate.ZONE_SHANGHAI);
        LocalDateTime openTime = merchantActivity.getOpenTime().toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        LocalDateTime overTime = merchantActivity.getOverTime().toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        if (now.compareTo(openTime) > 0) {
            throw new ServiceException(errMsgPrefix + "活动已经开始");
        }
        if (now.compareTo(overTime) > 0) {
            throw new ServiceException(errMsgPrefix + "活动已经结束");
        }
    }

    @Override
    public void saveIncomeExpend(MerchantActivity merchantActivity, Integer status) {
        //平台抽成
        SysParams sysParams = sysParamsService.getByIdForUpdate(1L);
        BigDecimal multiply = merchantActivity.getCost().multiply(new BigDecimal(sysParams.getValue()));
        BigDecimal profit = merchantActivity.getCost().subtract(multiply);
        //平台收入记录
        AdProfit adProfit = new AdProfit().setOrigin("平台服务费(活动报名)").setMoney(profit);
        if (!adProfitService.save(adProfit)) {
            throw new ServiceException(adProfit.toString());
        }
        //商家收入流水增加
        MerchantInfo merchantInfo = merchantInfoService.getByIdForUpdate(merchantActivity.getMerchantId());
        merchantInfo.setBalance(merchantInfo.getBalance().add(profit));
        MerchantIncomeExpend merchantIncomeExpend = new MerchantIncomeExpend().setMerchantId(merchantActivity.getMerchantId()).setMoney(profit).setStatus(1);
        if (!merchantInfoService.updateById(merchantInfo)) {
            throw new ServiceException(merchantInfo.toString());
        }
        if (!merchantIncomeExpendService.save(merchantIncomeExpend)) {
            throw new ServiceException(merchantIncomeExpend.toString());
        }
    }
}
